package convert

import (
	"errors"
	"math"
	"strconv"
)

// Int32ToInt 将int32类型转换为int类型，由于int32在int范围内，所以直接转换
func Int32ToInt(value int32) (int, error) {
	return int(value), nil
}

// Int32ToInt8 将int32类型转换为int8类型，先检查是否在int8范围内，然后进行转换
func Int32ToInt8(value int32) (int8, error) {
	if int64(value) < math.MinInt8 || int64(value) > math.MaxInt8 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int8(value), nil
}

// Int32ToInt16 将int32类型转换为int16类型，先检查是否在int16范围内，然后进行转换
func Int32ToInt16(value int32) (int16, error) {
	if int64(value) < math.MinInt16 || int64(value) > math.MaxInt16 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int16(value), nil
}

// Int32ToInt64 将int32类型转换为int64类型，由于int32在int64范围内，所以直接转换
func Int32ToInt64(value int32) (int64, error) {
	return int64(value), nil
}

// Int32ToUint 将int32类型转换为uint类型，先检查是否为负数，然后进行转换
func Int32ToUint(value int32) (uint, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint")
	}
	return uint(value), nil
}

// Int32ToUint8 将int32类型转换为uint8类型，先检查是否为负数，然后检查是否在uint8范围内，然后进行转换
func Int32ToUint8(value int32) (uint8, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint8")
	}
	if int64(value) > math.MaxUint8 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint8(value), nil
}

// Int32ToUint16 将int32类型转换为uint16类型，先检查是否为负数，然后检查是否在uint16范围内，然后进行转换
func Int32ToUint16(value int32) (uint16, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint16")
	}
	if int64(value) > math.MaxUint16 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint16(value), nil
}

// Int32ToUint32 将int32类型转换为uint32类型，先检查是否为负数，然后直接转换
func Int32ToUint32(value int32) (uint32, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint32")
	}
	return uint32(value), nil
}

// Int32ToUint64 将int32类型转换为uint64类型，先检查是否为负数，然后直接转换
func Int32ToUint64(value int32) (uint64, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint64")
	}
	return uint64(value), nil
}

// Int32ToFloat32 将int32类型转换为float32类型，直接转换
func Int32ToFloat32(value int32) (float32, error) {
	return float32(value), nil
}

// Int32ToFloat64 将int32类型转换为float64类型，直接转换
func Int32ToFloat64(value int32) (float64, error) {
	return float64(value), nil
}

// Int32ToString 将int32类型转换为string类型，使用strconv.Itoa函数进行转换
func Int32ToString(value int32) string {
	return strconv.Itoa(int(value))
}

// Int32ToBool 将int32类型转换为bool类型，非零值转换为true，零值转换为false
func Int32ToBool(value int32) bool {
	return value != 0
}
